import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}

const router=new Router({
    routes: [
      {
        path: '/',
        redirect: '/login'
      },
      {
        path: '/login',
        component: () => import( /* webpackChunkName: "login" */ '../views/Login.vue'),
        meta: { title: '登录' }
      },
      {
        path: '/homes',
        component: () => import( /* webpackChunkName: "Homes" */ '../views/Homes.vue'),
        redirect:"/home",
        meta: { title: '首页' },
        children:[
          {
            path: "/home",
            component:()=>import( /* webpackChunkName: "home" */ '../views/home.vue'),
            meta: { title: '首页' },
          },
          {
            path: "/questionManagement",
            component:()=>import( /* webpackChunkName: "questionManagement" */ '../views/questionManagement.vue'),
            meta: { title: '题目管理' }
          },
	  {
	    path: "/queryQuestion",
	    component:()=>import( /* webpackChunkName: "queryQuestion" */ '../views/queryQuestion.vue'),
	    meta: { title: '查看题目列表' }
	  },
          {
            path: "/addQuestion",
            component:()=>import( /* webpackChunkName: "addQuestion" */ '../views/addQuestion.vue'),
            meta: { title: '编辑（增删改）' }
          },
	  {
	    path: "/queryUtilizer",
	    component:()=>import( /* webpackChunkName: "queryUtilizer" */ '../views/queryUtilizer.vue'),
	    meta: { title: '查看用户列表' }
	  },
          {
            path: "/challengeRecords",
            component:()=>import( /* webpackChunkName: "challengeRecords" */ '../views/challengeRecords.vue'),
            meta: { title: '挑战记录' }
          },
	  {
	    path: "/scoreRecords",
	    component:()=>import( /* webpackChunkName: "scoreRecords" */ '../views/scoreRecords.vue'),
	    meta: { title: '积分记录' }
	  },
	  {
	    path: "/addStaff",
	    component:()=>import( /* webpackChunkName: "addStaff" */ '../views/addStaff.vue'),
	    meta: { title: '添加人员' }
	  },
	  {
	    path: "/queryStaff",
	    component:()=>import( /* webpackChunkName: "queryStaff" */ '../views/queryStaff.vue'),
	    meta: { title: '查询人员/角色' }
	  },
		
	  
         
        ]
      },

  ]
})

// 路由守卫
router.beforeEach((to,from,next)=>{
  // 动态改变页面title
  if(to.meta && to.meta.title){
    document.title = to.meta.title+"--馨金湖后台管理"
  }
  if(to.path==="/login") return next()
  const token=sessionStorage.getItem("token")
  if(!token) return next("/login")
  next()
})

export default router